﻿// ----------------------------------
// Depends on:
//   jQuery
//   Microsoft.Ajax
// ----------------------------------

/**
* GridView button column
* @param name name of group checkbox.
*/
function groupCheckBoxes(name) {
	if (!name)
		return;

	var trigger = jQuery("input:checkbox[name=" + name + "]");

	var group = name.substring(0, name.length - 4); // remove suffix '_all'.

	var checked = trigger.prop("checked");
	jQuery("input:checkbox:enabled[name=" + group + "]").prop("checked", checked);

	return true;
}

/**
* Set value of URL parameter.
* @param pn name of parameter.
* @param pv new value of parameter.
*/
function setURLParam(pn, pv) {
	var pp = purl().params();
	pp[pn] = pv;

	window.location.href = window.location.protocol + window.location.host + window.location.pathname + "?" + jQuery.param(pp);

	return false;
}

/**
*
*
*/
function myViewFiles_previewTxtFile(cntId, fileId) {
	var cnt = jQuery("#" + cntId);

	var cPre = jQuery("div.pre", cnt);

	return false;
}

/**
*
*
*/
function myViewFiles_previewImgFile(cntId, fileId) {
	var cnt = jQuery("#" + cntId);

	var cPre = jQuery("div.pre", cnt);

	return false;
}

/**
*
*
*/
function myManageFiles_addFile(cntId) {
	var cnt = jQuery("#" + cntId);

	var lastItem = jQuery("li:has(input:file):last", cnt);
	var newItem = lastItem.clone();

	var newFile = jQuery(":file", newItem);

	var seq = (parseInt(newItem.attr("seq")) + 1).toString();
	newItem.attr("seq", seq);
	newFile.attr("seq", seq);

	var name = newFile.prop("name");
	var l = name.lastIndexOf("_");
	newFile.prop("name", name.substring(0, l) + "_" + seq);

	var newDeleteNewButton = jQuery("input:image", newItem);
	newDeleteNewButton.attr("onclick", "return myManageFiles_deleteNewFile('" + cntId + "', " + seq + ");")

	newItem.insertAfter(lastItem);

	return false;
}

/**
*
*
*/
function myManageFiles_checkFile(cntId, seq) {
	var cnt = jQuery("#" + cntId);

	var cItem = jQuery("li[seq=" + seq + "]", cnt);

	var cbx = jQuery("input:checkbox:first", cItem);
	var lnk = jQuery("a:first", cItem);
	var ibt1 = jQuery(".deleteFile", cItem);
	var ibt2 = jQuery(".recoverFile", cItem);

	cbx.prop("checked", true);
	ibt1.hide();
	ibt2.show();

	lnk.addClass("removedFile");

	return false;
}

/**
*
*
*/
function myManageFiles_uncheckFile(cntId, seq) {
	var cnt = jQuery("#" + cntId);

	var cItem = jQuery("li[seq=" + seq + "]", cnt);

	var cbx = jQuery("input:checkbox:first", cItem);
	var lnk = jQuery("a:first", cItem);
	var ibt1 = jQuery(".deleteFile", cItem);
	var ibt2 = jQuery(".recoverFile", cItem);

	cbx.prop("checked", false);
	ibt2.hide();
	ibt1.show();

	lnk.removeClass("removedFile");

	return false;
}

/**
*
*
*/
function myManageFiles_deleteNewFile(cntId, seq) {
	var cnt = jQuery("#" + cntId);

	var cItem = jQuery("li[seq=" + seq + "]", cnt);

	var ef = jQuery(":file:enabled", cnt);
	if (ef.size() <= 1) {
		// Cannot delete the unique file.
		return false;
	}

	cItem.remove().empty();

	return false;
}

/**
* 
*
*/
function myHtmlEncode(s) {
	if (!s)
		return '';

	s = s.replace(/&/g, "&gt;")
		.replace(/</g, "&lt;")
		.replace(/>/g, "&gt;")
		.replace(/ /g, "&nbsp;")
		.replace(/\'/g, "&#39;")
		.replace(/\"/g, "&quot;")
		.replace(/\n/g, "<br>");

	return s;
}

/**
*
*
*/
function myViewFiles_previewTxtFile(cntId, url, title, title2) {
	var cnt = jQuery("#" + cntId);

	var previewBox = jQuery("#previewTxtBox");
	if (previewBox.size() == 0)
		return false;

	var previewBoxContent = jQuery("#previewTxtBoxContent", previewBox);

	var titleBar = jQuery("#previewTxtBoxTitle", previewBox);
	var titleText = jQuery("#previewTxtBoxTitleText", titleBar);
	titleText.text(title2);
	titleText.prop("title", title);

	my_showOverlay();

	var sw = document.documentElement.scrollWidth || document.body.scrollWidth;
	var sh = document.documentElement.scrollHeight || document.body.scrollHeight;
	var cw = document.documentElement.clientWidth || document.body.clientWidth;
	var ch = document.documentElement.clientHeight || document.body.clientHeight;
	var st = document.documentElement.scrollTop || document.body.scrollTop;

	var mw = cw * .8;
	var mh = ch * .8;
	var ml = (sw - mw) / 2; // left
	var mt = st + (ch - mh) / 2;  // top

	var contentPadding = 4;
	previewBoxContent.css('width', (mw - 2 * contentPadding) + 'px');
	previewBoxContent.css('height', (mh - 2 * contentPadding - titleBar.height()) + 'px');
	previewBoxContent.css('left', contentPadding + 'px');
	previewBoxContent.css('top', (contentPadding + titleBar.height()) + 'px');

	previewBox.css('width', mw + 'px');
	previewBox.css('height', mh + 'px');
	previewBox.css('left', ml + 'px');
	previewBox.css('top', mt + 'px');
	previewBox.show();

	previewBoxContent.addClass("loading");

	var pre = jQuery("#previewTxtBoxText", previewBoxContent);
	jQuery.ajax({
		url: url,
		type: 'POST',
		data: { timestamp: new Date().getTime() },
		dataType: 'text'
	}).done(function (responseText) {
		var npre = jQuery('<PRE>\n\n' + myHtmlEncode(responseText) + '</PRE>');
		npre.prop('id', pre.prop('id'));
		npre.css('display', 'none');
		npre.insertAfter(pre);
		pre.remove().empty();

		previewBoxContent.removeClass("loading");
		previewBoxContent.addClass("loaded");

		npre.show();
	});

	var closeBtn = jQuery('#previewTxtBoxTitleClose', titleBar);
	closeBtn.click(function () {
		pre.hide(function () {
			pre.html('');
		});
		previewBox.hide();
		my_hideOverlay();
	});

	return false;
}

/**
*
*
*/
function myViewFiles_previewImgFile(cntId, url, title, title2) {
	var cnt = jQuery("#" + cntId);

	var previewBox = jQuery("#previewImgBox");
	if (previewBox.size() == 0)
		return false;

	var previewBoxContent = jQuery("#previewImgBoxContent", previewBox);

	var titleBar = jQuery("#previewImgBoxTitle", previewBox);
	var titleText = jQuery("#previewImgBoxTitleText", titleBar);
	titleText.text(title2);
	titleText.prop("title", title);

	my_showOverlay();

	var sw = document.documentElement.scrollWidth || document.body.scrollWidth;
	var sh = document.documentElement.scrollHeight || document.body.scrollHeight;
	var cw = document.documentElement.clientWidth || document.body.clientWidth;
	var ch = document.documentElement.clientHeight || document.body.clientHeight;
	var st = document.documentElement.scrollTop || document.body.scrollTop;

	var mw = cw * .8;
	var mh = ch * .8;
	var ml = (sw - mw) / 2; // left
	var mt = st + (ch - mh) / 2;  // top

	var contentPadding = 4;
	previewBoxContent.css('width', (mw - 2 * contentPadding) + 'px');
	previewBoxContent.css('height', (mh - 2 * contentPadding - titleBar.height()) + 'px');
	previewBoxContent.css('left', contentPadding + 'px');
	previewBoxContent.css('top', (contentPadding + titleBar.height()) + 'px');

	previewBox.css('width', mw + 'px');
	previewBox.css('height', mh + 'px');
	previewBox.css('left', ml + 'px');
	previewBox.css('top', mt + 'px');
	previewBox.show();

	previewBoxContent.addClass("loading");

	var img = jQuery('#previewImgBoxImage', previewBoxContent);
	img.prop('src', url).load(function () {
		if (img.height() < previewBoxContent.height()) {
			var imt = (previewBoxContent.height() - img.height()) / 2;
			img.css('margin-top', imt + 'px');
		} else {
			img.css('margin-top', '0');
		}

		previewBoxContent.removeClass("loading");
		previewBoxContent.addClass("loaded");
		img.show();
	});

	var closeBtn = jQuery('#previewImgBoxTitleClose', titleBar);
	closeBtn.click(function () {
		img.hide(function () {
			img.prop('src', '');
		});
		previewBox.hide();
		my_hideOverlay();
	});

	return false;
}

/**
*
*
*/
function my_hideMe(obj, settings) {
	settings = jQuery.extend({ speed: 'normal', callback: null }, settings);

	var me = jQuery(obj);

	me.fadeOut(settings.speed, settings.callback);

	return false;
}

/**
*
* @param source Validator.
* @param arguments Arguments.
*/
function my_validateYMD(source, arguments) {
	try {
		var bits = jQuery.map(arguments.Value.split(/\D+/), function (itm, i) { return parseInt(itm, 10) });
		bits[1] -= 1;
		if (bits.length < 3) {
			bits.push(1);
		}
		day = new Date(bits[0], bits[1], bits[2]);
		arguments.IsValid = (day.getMonth() == bits[1] && day.getFullYear() == bits[0]
			&& day.getDate() == bits[2]);
	}
	catch (err) {
		arguments.IsValid = false;
	}
}

function my_showOverlay() {
	var sw = document.documentElement.scrollWidth || document.body.scrollWidth;
	var sh = document.documentElement.scrollHeight || document.body.scrollHeight;

	var ol = jQuery("#panelOverlay");
	ol.width(sw);
	ol.height(sh);
	ol.show();
}

function my_showUpdateMessage() {
	var sw = document.documentElement.scrollWidth || document.body.scrollWidth;
	var sh = document.documentElement.scrollHeight || document.body.scrollHeight;
	var ch = document.documentElement.clientHeight || document.body.clientHeight;
	var st = document.documentElement.scrollTop || document.body.scrollTop;

	var msg = jQuery("#updatePanelMessage");
	var ml = (sw - msg.width()) / 2; // left
	var mt = st + (ch - msg.height()) / 2;  // top
	msg.css('left', ml + 'px');
	msg.css('top', mt + 'px');
	msg.show();
}

function my_hideOverlay() {
	var ol = jQuery("#panelOverlay");
	ol.hide();
}

function my_hideUpdateMessage() {
	var msg = jQuery("#updatePanelMessage");
	msg.hide();
}

Sys.Application.add_init(function () {
	Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(function (sender, e) {
		my_showOverlay();
		my_showUpdateMessage();
	});

	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
		my_hideUpdateMessage();
		my_hideOverlay();
	});

	Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function (sender, e) {
	});
});

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
